package com.benshou.bcss.common.feign.config;

import com.benshou.bcss.common.feign.config.props.FeignOkhttpProps;
import lombok.extern.slf4j.Slf4j;
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Feign 配置
 *
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
@Configuration
@Slf4j
public class FeignConfig {

    /**
     * @param feignOkhttpProps Feign 使用的 OkHttp 客户端的属性
     * @return Feign 使用的 OkHttp 客户端
     */
    @Bean
    public okhttp3.OkHttpClient okHttpClient(FeignOkhttpProps feignOkhttpProps) {
        return new okhttp3.OkHttpClient.Builder()
                .connectTimeout(feignOkhttpProps.getConnectTimeout())
                .readTimeout(feignOkhttpProps.getReadTimeout())
                .writeTimeout(feignOkhttpProps.getWriteTimeout())
                .addInterceptor(new HttpLoggingInterceptor(logger::debug)
                        .setLevel(
                                logger.isDebugEnabled() ?
                                        HttpLoggingInterceptor.Level.BODY :
                                        HttpLoggingInterceptor.Level.NONE))
                .build();
    }

    // todo 应该不需要注册，待验证
//    @Bean
//    public feign.okhttp.OkHttpClient feignClient(okhttp3.OkHttpClient okHttpClient) {
//        return new feign.okhttp.OkHttpClient(okHttpClient);
//    }
}
